package com.dai.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.dai.train.common.exception.BusinessException;
import com.dai.train.common.exception.BusinessExceptionEnum;
import com.dai.train.common.util.JwtUtil;
import com.dai.train.common.util.SnowUtil;
import com.dai.train.member.domain.Member;
import com.dai.train.member.domain.MemberExample;
import com.dai.train.member.mapper.MemberMapper;
import com.dai.train.member.req.MemberLoginReq;
import com.dai.train.member.req.MemberRegisterReq;
import com.dai.train.member.req.MemberSendCodeReq;
import com.dai.train.member.resp.MemberLoginResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dai.train.common.exception.BusinessExceptionEnum.MEMBER_CODE_ERROR;
import static com.dai.train.common.exception.BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST;

@Service
public class MemberService {

    Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    private MemberMapper memberMapper;

    public int getMemberCount(){
        return Math.toIntExact(memberMapper.countByExample(null));
    }


    public long register(MemberRegisterReq req){


        // 先根据手机号码查询用户
        MemberExample memberExample = new MemberExample();
        Member memberDB = selectMemberByMobile(req.getMobile(), memberExample);

        if(ObjectUtil.isNotEmpty(memberDB)){
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }


        // 注册手机
        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(req.getMobile());
        memberMapper.insert(member);
        return member.getId();
    }

    public void sendCode(MemberSendCodeReq req){

        // 先根据手机号码查询用户
        MemberExample memberExample = new MemberExample();
        Member memberDB = selectMemberByMobile(req.getMobile(), memberExample);

        if(ObjectUtil.isEmpty(memberDB)){
            // 记录为空，插入记录
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(req.getMobile());
            memberMapper.insert(member);
            LOG.info("记录为空，插入记录：{}",member);
        }

        // 生成验证码
        String code = RandomUtil.randomString(4);
        LOG.info("生成验证码{}",code);

        // 保存短信记录表：手机号、短信验证码、有效期、是否已使用、业务类型、发送时间、使用时间


        // 对接短信通道，发送短信

    }

    public MemberLoginResp login(MemberLoginReq req){

        // 先根据手机号码查询用户
        MemberExample memberExample = new MemberExample();
        Member memberDB = selectMemberByMobile(req.getMobile(), memberExample);

        if(ObjectUtil.isEmpty(memberDB)){
            // 记录为空
            throw new BusinessException(MEMBER_MOBILE_NOT_EXIST);
        }

        if(!"8888".equals(req.getCode())){
            throw new BusinessException(MEMBER_CODE_ERROR);
        }
        MemberLoginResp resp = new MemberLoginResp();
        BeanUtil.copyProperties(memberDB,resp);

        // 下发 token
        String token = JwtUtil.createToken(resp.getId(), resp.getMobile());
        resp.setToken(token);

        return resp;

    }

    private Member selectMemberByMobile(String mobile, MemberExample memberExample) {
        memberExample.createCriteria().andMobileEqualTo(mobile);
        List<Member> list = memberMapper.selectByExample(memberExample);
        if(CollectionUtil.isNotEmpty(list)){
            return list.get(0);
        }
        return null;
    }
}
